package com.znyj.servlet.utilsTool;

import com.alibaba.fastjson.JSONObject;
import com.znyj.servlet.entity.FactoryEntity;
import com.znyj.servlet.thrift.ThriftClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

/**
 * Created by gaoliwei02 on 17/6/29.
 */
public class CommonTool {
    private static final Logger logger = LoggerFactory.getLogger(CommonTool.class);

    public static boolean isNullOrInvalid(String s) {
        if (s == null) {
            return true;
        }
        if (s.equals("")) {
            return true;
        }
        return false;
    }

    /*
    * /crypt getDeviceInfo openDevice getOrderInfo
    * /sign logout index
    * /factory
    * /demo index
    * /test
    * /system index
    *
    *
    * /merchant bind modify/{poiid}
    *
    * /user index
    *
    * /report index
    * /task index
    *
    * /single {device_id}

    *
    * */

//    1
    public static Pattern superAdmin = Pattern.compile("" +
                    "^/*"
    );

//    2
    public static Pattern commonAdmin = Pattern.compile("" +
                    "^/merchant/\\w*|" +
                    "^/user/\\w*|" +
                    "^/report/\\w*|" +
                    "^/task/\\w*|" +
                    "^/single/\\w*"
    );

//    3
    public static Pattern bd = Pattern.compile("" +
                    "^/merchant/\\w*|" +
                    "^/report/\\w*|" +
                    "^/task/\\w*"
    );

////    4
//    public static Pattern merchant = Pattern.compile("" +
//                    "^/merchant/\\w*"
//    );

    public static JSONObject updateListSend(List<String> deviceList, String userId) {
        JSONObject recjson = null;
        JSONObject resjson = new JSONObject();

        for (int i = 0; i < deviceList.size(); i++) {
            recjson = ThriftClient.updateSendThriftApi(userId, deviceList.get(i), "up");
            resjson.put(recjson.getString("deviceId"), recjson.getString("status"));
        }
        return resjson;
    }

    public static boolean permissionCheck(Integer rolePermission, String url) {
        boolean res = false;
        switch ((rolePermission)) {
            case 1: {
                res = superAdmin.matcher(url).find();
                break;
            }

            case 2: {
                res = commonAdmin.matcher(url).find();
                break;
            }

            case 3: {
                res = bd.matcher(url).find();
                break;
            }

//            case 4: {
//                res = merchant.matcher(url).find();
//                break;
//            }
        }
        return res;
    }

    public static AtomicInteger getSequenceId() {
        return sequenceId;
    }

    private static AtomicInteger sequenceId = new AtomicInteger(0);

    public static int incSequenceId() {
        return sequenceId.incrementAndGet();
    }

    public static int swapSequenceId(int newValue) {
        return sequenceId.getAndSet(newValue);
    }

    private static String lastWeek = "";

    public static String createDeviceId(FactoryEntity factoryEntity) {

        String A = factoryEntity.getDeviceType();
        A = A.substring(0, A.indexOf('.'));


        String B = factoryEntity.getCommType();
        B = B.substring(0, B.indexOf('.'));


        String C = factoryEntity.getCountry();
        C = C.substring(0, C.indexOf('.'));

        SimpleDateFormat sdf = new SimpleDateFormat("yy");
        Date date = new Date();
        String formatDate = sdf.format(date);

        Calendar c = Calendar.getInstance();
        String week = String.valueOf(c.get(Calendar.WEEK_OF_YEAR));
        //新的一个星期
        if (!lastWeek.equals(week)) {
            swapSequenceId(0);
        }
        lastWeek = week;


        int atomicFactoryNum = incSequenceId();
        logger.info("Create sequenceId: " + CommonTool.getSequenceId());
        String sequence = String.format("%05d", atomicFactoryNum);

        String fnum = A + B + C + formatDate + week + sequence;
        return fnum;
    }

    public static String parseCtrlMsg(int port, int actionMsg, String controlMsg) {
        if (actionMsg == 0) {
            if (port == 0) {
                return "cdbaopen";
            } else if (port == 1) {
                return "cdbbopen";
            } else if (port == 2) {
                return "cdbopen";
            }
        } else if (actionMsg == 1) {
            if (port == 0) {
                return "cdbaclose";
            } else if (port == 1) {
                return "cdbbclose";
            } else if (port == 2) {
                return "cdbclose";
            }
        } else if (actionMsg == 2) {
            return controlMsg;
        }
        return "error";
    }
}
